<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
		<title>登录绑定</title>
		<link rel="stylesheet" type="text/css" href="css/public.css"/>
	</head>
	<style type="text/css">
		/*移动端1px下边线*/
		.border{ position: relative; }
		.border:after{ content: ''; display:block; position: absolute; width: 100%; left: 0px; bottom: 0px;height: 1px; background-color:  #efefef; -webkit-transform: scaleY(0.5); transform: scaleY(0.5);}
		body{padding-top: 1.38rem;}
		.icon{font-size:1.7rem;margin-left: 2.6rem;}
		.container{height: auto;background: #fff;margin-top: 0.35rem;padding: 0.4rem 0;}
		.container .item{height: 0.87rem;margin-left: 0.3rem;font-size: 0.3rem;}
		.container .item input{margin-left: 0.7rem;}
		.confirm{height: 0.88rem;width: 6.8rem;margin: 1.2rem auto;font-size: 0.3rem;background: #0D6FB8;border-radius: 5px;color: #fff;cursor: pointer;}
		.verification-btn{width: 1.8rem;height: 0.6rem;border:solid #a3a3a3 1px;border-radius: 3px;font-size: 0.28rem;}
	</style>
	<body>
		<i class="icon iconfont icon-bangding"></i>
		<div class="container">
			<div class="item border flex flex-align-center" >
				<p>账号</p>
				<input type="text" placeholder="请输入手机号" id="phone"/>
			</div>
			<div class="passwordbox" hidden>
			<div class="item border flex flex-align-center border">
				<p>密码</p>
				<input type="password" placeholder="请输入优鸟钱包密码" id="password"/>
			</div>
			</div>
			<div class="item  flex flex-align-center">
				<input style="margin-left: 1.3rem;" type="text" placeholder="请输入验证码" id="verification"/>
				<div class="verification-btn flex flex-align-center flex-pack-center"><p>获取验证码</p></div>
			</div>
			<div class="passwordbox" hidden>
			<div class="item flex flex-align-center">
				<p>推荐码</p>
				<input type="text" placeholder="请输入推荐码" style="margin-left: 0.4rem;" id="reCode"/>
			</div>
			</div>
		</div>
		<p style="font-size: 0.26rem;margin-left: 0.3rem;margin-top: 0.2rem;">提示：以上账号密码均为优鸟钱包的账号密码</p>
		<div class="confirm flex flex-align-center flex-pack-center"><p style="margin-left: 0;">确认提交</p></div>
	</body>
	<script type="text/javascript" src="js/rem.js"></script>
	<script type="text/javascript" src="js/zepto.js"></script>
	<script type="text/javascript" src="js/template.js"></script>
	<script type="text/javascript" src="js/api.js"></script>
	<script type="text/javascript" src="js/common.js"></script>
	<script type="text/javascript" src="js/layer.js"></script>
	<script type="text/javascript" src="js/jquery.base64.js"></script>
	<script type="text/javascript">
			var webPath = 'http://www.uniao.me';
			// 动态验证手机号是否已经注册
			$('#phone').keyup(function(){
				var phone = $('#phone').val(); // 手机号
				if(phone.match(/^(1)[0-9]{10}$/)){
					$.ajax({
						type: 'post',
						url: webPath + '/app/user/checkExist.htm',
						contentType: 'application/json; charset=utf-8',
						data: JSON.stringify({"phone": phone}),
						dataType: 'json',
						success: function(data){
							if (data.code == 50006){
								//手机号码已注册,获取绑定验证码,可以进行绑定操作
								$(".passwordbox").hide();
								$("#verification").parent('.item').removeClass('border');
								$('.verification-btn').on('click', function(){verify(this)});
							} else if(data.code == 200) {
								//验证到号码未注册，获取注册验证码
								$(".passwordbox").show();
								$("#verification").parent('.item').addClass('border');
								//信息框
								  layer.open({
								    content: '您并没有注册优鸟钱包,您可以注册后使用'
								    ,btn: '我知道了'
								  });
								 //手机符合规则，给获取验证码按钮绑定事件
								$('.verification-btn').on('click', function(){verify(this)});
							}
						}
					});
				}
			});
			// 如果手机号符合规则,可以点击获取验证码 开始倒计时
			function verify(self) {
				var phone = $('#phone').val(); // 手机
				if(!phone.match(/^(1)[0-9]{10}$/)){
					layer.open({
					  	content: '手机格式不正确',
					  	skin: 'msg',
					  	time: 2
					});
					return false;
				}
				// ajax请求发送验证码
				$.ajax({
					type: 'post',
					url: webPath + '/common/sms/sendVerify.htm',
					contentType: 'application/json; charset=utf-8',
					data: JSON.stringify({'phone': phone, bizType: 0}),
					dataType: 'json',
					success: function(data){
						console.log(data);
						if (data.code == 200) {
							layer.open({
							  	content: '验证码已发送',
							  	skin: 'msg',
							  	time: 2
							});
							$('.verification-btn').off('click', function(){verify(this)});
						}else {
							layer.open({
							  	content: data.message,
							  	skin: 'msg',
							  	time: 2
							})
						}
					}
				});
				var time = 60;
				$(self).css('background-color','#f0f0f0');
				$(self).off('click');
				var timer = setInterval(function () {
					if(time == 1){
						time = 60;
						$(self).text('重新发送');
						$(self).css('backgroundColor', '#fff');
						clearInterval(timer);
						$(self).on('click', function(){verify(this)});
					}else{
						time--;
						$(self).text(time + 's后重试');
					}
				},1000);
			};
		//确认提交响应事件
		$('.confirm').on('click',function(){
			var phone = $('#phone').val();
			var pwd = $('#password').val();
			var verifyCode = $('#verification').val();
			var reCode = $('#reCode').val();
			if(!phone.match(/^(1)[0-9]{10}$/)){
				layer.open({
					  	content: '手机格式不正确',
					  	skin: 'msg',
					  	time: 3
				});
			}else if($('.passwordbox').css('display') == 'block' && $.trim(pwd) == ''){
					layer.open({
					  	content: '请填写登录密码',
					  	skin: 'msg',
					  	time: 3
			 });
			}else if(!verifyCode){
					layer.open({
					  	content: '请输入验证码',
					  	skin: 'msg',
					  	time: 3
					})
			}else{			
				Public.regster({
					openId: localStorage.openid,
					clientType: 'WX',
					phone: phone,
					password: $.base64.encode(pwd),
					verifyCode: verifyCode,
					reCode: reCode	
				},function(res){
					layer.open({
					  	content: res.message,
					  	skin: 'msg',
					  	time: 3
					});
					setTimeout(function() { //绑定成功后跳转到之前想请求的页面
						Util.jump(localStorage.url);
					},3000);
				});
		}
		});	
	</script>
</html>
